<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"  
               :search.sync="query"
               ref="crud" 
               id="crud"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuRight">
        <table-right :crud='$refs.crud' :data='data' :refreshChange='refreshChange'>
        </table-right>
      </template>
      <template slot-scope="{row}" slot="menu" width="360px">
        <el-button   size="small" type="text"  @click="enableFn(row)" v-if="row.enabled == false" >上线</el-button> 
        <el-button   size="small" type="text"  @click="unableFn(row)" v-if="row.enabled == true" >下线</el-button> 
        <el-button   size="small" type="text"  @click="stopFun(row)"  >停止</el-button>
      </template>   
    </avue-crud> 
  </basic-container>
</template>

<script>
  import * as api from "@/api/upgrade/nacos";
  import {mapGetters} from "vuex"; 
  import {init} from '@/mixins/init';  
  export default { 
    mixins: [init],
    data() {
      return {
        form: {}, 
        query: {
          serviceName:'blade-auth'
        },
        loading: true,
        page: {
          pageSize: 100,
          currentPage: 1,
          total: 0
        },
        title:'升级配置',
        showDialog: false,
        openType:'add',
        clickRow:null,
        selectionList: [],
        option: { 
          dialogWidth: '1000',
          labelWidth: 120,
          searchGutter: 0,
          menuWidth: 120,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: false,
          editBtn: false,
          addBtn: false,
          delBtn: false,
          selection: true,
          menu:true,
          column: [
           {
              label: '工程',
              prop: 'serviceName', 
              type:'select',
              search: true,
              dicUrl:"/api/nacos/dicList",
              props: {
                label: "configName",
                value: "configId"
              },
            },
            {
              label: "IP",
              prop: "ip",
              search: false, 
            },
            {
              label: "端口",
              prop: "port" ,  
            }, 
            {
              label: "状态",
              prop: "enabled" ,  
            } 
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: false,
          viewBtn: false,
          delBtn: false,
          editBtn: false
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      enableFn(row){
        this.$confirm("确定上线?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => { 
          return api.enable(row);
        })
        .catch(()=>{}) // 添加错误捕获
        .then((res) => {  
          if(res && res.data && res.data.msg){
            this.$message({
              type: "success",
              message: res.data.msg
            });
          } 
          var that = this;
          window.setTimeout(function() {
            that.onLoad(that.page);
          }, 2000);  
        });         
      },
      unableFn(row){
        this.$confirm("确定下线?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => { 
          return api.unable(row);
        })
        .catch(()=>{}) // 添加错误捕获
        .then((res) => {   
          if(res && res.data && res.data.msg){
            this.$message({
              type: "success",
              message: res.data.msg
            });
          }
          var that = this;
          window.setTimeout(function() {
            that.onLoad(that.page);
          }, 2000);                   
        });                 
      },
      stopFun(row){
        this.$confirm("确定停止该服务?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => { 
          return api.stop(row);
        })
        .catch(()=>{}) // 添加错误捕获
        .then((res) => {   
          if(res && res.data && res.data.msg){
            this.$message({
              type: "success",
              message: res.data.msg
            });
          }
          var that = this;
          window.setTimeout(function() {
            that.onLoad(that.page);
          }, 2000);                   
        });                 
      },     
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;  
        console.log(this.query)
        if(!this.query.serviceName){
          this.page.total = 0;
          this.data = [];
          this.loading = false; 
          return;
        }        
        api.getInstances(this.query).then(res => { 
          const records = res.data.data; 
          if(records){
            this.page.total = records.length;
            this.data = records;
          } else {
            this.page.total = 0;
            this.data = [];
          }         
          this.loading = false; 
        });
      }
    }
  };
</script>

<style>
</style>
